<?php

namespace services\infusion;

use common\components\Service;
use common\helpers\DateHelper;
use common\models\patient\InfusionPrintLog;
use common\models\patient\InfusionQueue;
use Yii;
use yii\db\Expression;

/**
 * Class PrintLogService
 * @package services\infusion
 */
class PrintLogService extends Service
{
    /**
     * 新增打印日志
     * @param InfusionQueue $queue
     * @param int $type
     * @param array $drugConfigIds
     * @return InfusionPrintLog
     * @throws \Exception
     */
    public static function create(InfusionQueue $queue, int $type, array $drugConfigIds): InfusionPrintLog
    {
        foreach ($drugConfigIds as $key => $row) {
            $drugConfigIds[$key] = (int)$row;
        }
        sort($drugConfigIds);
        $infusionPrintLog = new InfusionPrintLog();
        $infusionPrintLog->project_id = $queue->project_id;
        $infusionPrintLog->patient_user_id = $queue->patient_user_id;
        $infusionPrintLog->queue_id = $queue->id;
        $infusionPrintLog->drug_config_ids = $drugConfigIds;
        $infusionPrintLog->type = $type;
        $infusionPrintLog->operate_id = Yii::$app->user->identity->member_id;
        $infusionPrintLog->saveAndCheckResult();
        return $infusionPrintLog;
    }

    /**
     * 获取患者最后一次打印日志
     * @param int $projectId
     * @param int $patientId
     * @param array $drugConfigIds
     * @return InfusionPrintLog|null
     */
    public static function getLastPrint(int $projectId, int $patientId, array $drugConfigIds): ?InfusionPrintLog
    {
        foreach ($drugConfigIds as $key => $row) {
            $drugConfigIds[$key] = (int)$row;
        }
        sort($drugConfigIds);
        $drugConfigIdsStr = json_encode($drugConfigIds);
        $today = DateHelper::today();
        /**
         * @var InfusionPrintLog|null $res
         */
        $res = InfusionPrintLog::find()
            ->where([
                'project_id' => $projectId,
                'patient_user_id' => $patientId,
            ])
            ->andWhere(['drug_config_ids' => new Expression("CAST('{$drugConfigIdsStr}' AS JSON)")])
            ->andWhere(['between', 'created_at', $today['start'], $today['end']])
            ->orderBy(['created_at' => SORT_DESC, 'id' => SORT_DESC])
            ->one();
        return $res;
    }

    /**
     * 是否已打印
     * @param int $projectId
     * @param int $patientId
     * @param array $drugConfigIds
     * @return array
     */
    /*public static function isPrint(int $projectId, int $patientId, array $drugConfigIds): array
    {
        $res = [
            'is_print_py' => false,
            'is_print_xs' => false,
        ];
        $log = self::getLastPrint($projectId, $patientId, $drugConfigIds);
        if (empty($log)) {
            return $res;
        }
        $queue = InfusionQueue::findOne($log->queue_id);
        if ($queue->status == InfusionQueue::STATUS_FINISH) {
            return $res;
        }
        $res['is_print_py'] = $queue->print_py_num > 0 ? true : false;
        $res['is_print_xs'] = $queue->print_xs_num > 0 ? true : false;
        return $res;
    }*/
}